/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package plar;

/**
 *
 * @author spok
 */
import java.awt.Point;
import java.util.HashMap;
import java.util.Vector;

import java.awt.Rectangle;
import plar.Properties.*;



public class Level
{
	public Vector <Element> elements;
        public String description;
	public HashMap <String,String> constants;
        public HashMap <String,Property> properties;
        
        public void setConstant(String k,String v)
        {
          constants.put(k, v);
        }
        
        public void addProperty(Property p)
        {
            p.level = this;
            properties.put(p.name, p);
        }
        
        public Property getProperty(String name)
        {
           return properties.get(name);
        }
	public Level(String n)
	{
		
                elements = new Vector <Element>();
                constants  = new HashMap <String,String>();
                properties = new HashMap <String,Property>() ;
                setConstant("name",n);
                addProperty(new CollisionDetect());
                addProperty(new MoveElements());
                addProperty(new Gravity(5));
        }
	
        
        
	public Level(String n,int r)
	{
                this(n);
		
                elements = new Vector <Element>();
                setConstant("msProperty",String.valueOf(r));
                LevelTimer thread = new LevelTimer ();
                thread.level=this;
                thread.msRefresh=r;
                thread.start();
	}
        
      public void clock()
      {
          for (Element e1: elements ) {
              for(String key:properties.keySet())      
              {
                  callProperty(key,e1);
              }
              for (Element e2: elements ) {
                  for(String key:properties.keySet())      
                  {
                      callProperty(key,e1,e2);
                  }
                   
              }
          }
      }
        
 
 
      	public Element addElement(Element e)
	{
	 elements.add(e);
         return e;
	}
    
      
        public Vector <Element> showScreen(Element player)
        {
          
            Rectangle pos = player.getPosition();
            Rectangle screen = new Rectangle(new Point(pos.x-200,pos.y-200));
            screen.width=800;
            screen.height=600;
            System.out.println("showScreen["+screen.x+","+screen.y+"] x["+(screen.width+screen.x)+","+(screen.height+screen.y)+"]");
            return showPortion(screen);
        }
        
        public Vector <Element> showPortion(Rectangle screen)
	{
		Vector <Element> shownElements=new Vector <Element>();
		for (Element e: elements ) {
                Rectangle pos = e.getPosition();
		if(pos.intersects(screen))
			shownElements.add(e);
		}
		
		return shownElements;
	}
        
        public void userInput(Element e,UserInput i)
	{
            Key keys = i.keys;
            Mouse mouse = i.mouse;
           
            HashMap trace = new HashMap();
            trace.put("source",e);
            trace.put("input",i);
            e.getAction("input").doAction(new Event(trace));
        }
        
        public boolean callProperty(String name)
        {
            return getProperty(name).doProperty();
        }

        public boolean callProperty(String name,Element e)
        {
            return getProperty(name).doElementProperty(e);
        }
        public  boolean callProperty(String name,Element e1,Element e2)
        {
           return  getProperty(name).doElementsProperty(e1, e2);
        }

}
